package com.wpp.controller;

import com.wpp.domain.User;
import com.wpp.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * user 的控制器
 * @author 吴鹏鹏ppp
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/signOut")
    public void signOut(HttpSession session, HttpServletResponse resp) throws IOException {
        System.out.println("我是同事b")    
		System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        //再次持久化cookie
        HttpSession session = req.getSession();
        System.out.println("获取登录"+session.getId());
        session.setMaxInactiveInterval(60*60*24*7);
        Cookie cookie = new Cookie("JSESSIONID", session.getId());
        cookie.setMaxAge(60*60*24*7);
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");
        System.err.println("我是同事b");



}

    @RequestMapping("/register")
    public Map<String, Object> register(User user){

        //返回的map
        HashMap<String, Object> map = new HashMap<>(4);
        map.put("error",false);
        map.put("message","注册成功!");
        userService.register(user);
        return map;
    }

    @RequestMapping("/login")
    public Map<String, Object> login(User user, HttpServletRequest req, HttpServletResponse resp){
        //清除session中的user信息
        HttpSession session = req.getSession();
        session.removeAttribute("loginUser");
        Cookie cookie = new Cookie("JSESSIONID", session.getId());

        Map<String, Object> map = userService.login(user);
        //登录成功
        if (!(Boolean) map.get("error")){
            //将用户存入session
            System.out.println("登录"+session.getId());
            session.setMaxInactiveInterval(60*60*24*7);
            session.setAttribute("loginUser",map.get("user"));
            cookie.setMaxAge(60*60*24*7);
            cookie.setPath("/");
            resp.addCookie(cookie);
        }

        return map;
    }

    @RequestMapping("/getLoginUser")
    public User getLoginUser(HttpServletRequest req, HttpServletResponse resp){
        //再次持久化cookie
        HttpSession session = req.getSession();
        System.out.println("获取登录"+session.getId());
        session.setMaxInactiveInterval(60*60*24*7);
        Cookie cookie = new Cookie("JSESSIONID", session.getId());
        cookie.setMaxAge(60*60*24*7);
        cookie.setPath("/");
        resp.addCookie(cookie);

        User loginUser = (User) session.getAttribute("loginUser");
        return loginUser;
    }
}
